import { ActionContext } from 'vuex'
import { IUserState } from './state'
import { login } from '@/api/system/user'
import { ACCESS_TOKEN, CURRENT_USER } from '@/store/mutation-types'
import { Storage } from '@/utils/storage'
import { IStore } from '@/store/types'

export const actions = {
  // 企业用户登录
  async login({ commit }: ActionContext<IUserState, IStore>, userInfo) {
    try {
      const response = await login(userInfo)
      const { result, code } = response
      if (code == '10000') {
        Storage.set(ACCESS_TOKEN, result.token)
        Storage.set(CURRENT_USER, result)
        commit('setUserInfo', result)
        commit('setToken', result.token)
        // todo
      }
      return Promise.resolve(response)
    } catch (e) {
      return Promise.reject(e)
    }
  },


  // 登出
  async logout({ commit }) {
    console.log(commit)
    commit('setToken', [])
    commit('setUserBizType', '')
    commit('asyncRoute/setCurActive', null, { root: true })
    Storage.remove(ACCESS_TOKEN)
    Storage.remove(CURRENT_USER)
    return Promise.resolve('')
  }
}
